#include <stdlib.h>
#include <stdio.h>
#include <SDL/SDL.h>
#include "fonction.h"

void menu(SDL_Surface *ecran)

{   int t1[2][3];
        t1[0][0]=100;
        t1[0][1]=200;
        t1[0][2]=300;
        t1[1][0]=100;
        t1[1][1]=200;
        t1[1][2]=300;
    SDL_Rect position,position_menu,posp,posm;
    SDL_Event event;
    int m,a,c,j,j2,l,v,k;
    int continuer = 1;

   // Mix_Music *musique;

    // const char* musiquePath = "musique.mp3";


    SDL_Surface* menu_bmp = SDL_LoadBMP("jouer.bmp");
    SDL_Surface* menu1_bmp = SDL_LoadBMP("aide.bmp");
    SDL_Surface* menu2_bmp = SDL_LoadBMP("quitter.bmp");
    SDL_Surface* aide_bmp = SDL_LoadBMP("aide1.bmp");
    SDL_Surface* niveau1_bmp = SDL_LoadBMP("cryptage.bmp");
    SDL_Surface* niveau2_bmp = SDL_LoadBMP("decryptage.bmp");
    SDL_Surface* jeu1_bmp = SDL_LoadBMP("jeu1.bmp");
    SDL_Surface* jeu11_bmp = SDL_LoadBMP("jeu1.bmp");
    SDL_Surface* a_bmp = SDL_LoadBMP("a.bmp");
    SDL_Surface* b_bmp = SDL_LoadBMP("b.bmp");
    SDL_Surface* c_bmp = SDL_LoadBMP("c.bmp");
    SDL_Surface* d_bmp = SDL_LoadBMP("d.bmp");
    SDL_Surface* e_bmp = SDL_LoadBMP("e.bmp");
    SDL_Surface* f_bmp = SDL_LoadBMP("f.bmp");
    SDL_Surface* g_bmp = SDL_LoadBMP("g.bmp");
    SDL_Surface* h_bmp = SDL_LoadBMP("h.bmp");
    SDL_Surface* i_bmp = SDL_LoadBMP("i.bmp");
    SDL_Surface* j_bmp = SDL_LoadBMP("j.bmp");
    SDL_Surface* k_bmp = SDL_LoadBMP("k.bmp");
    SDL_Surface* l_bmp = SDL_LoadBMP("l.bmp");
    SDL_Surface* m_bmp = SDL_LoadBMP("m.bmp");
    SDL_Surface* o_bmp = SDL_LoadBMP("o.bmp");
    SDL_Surface* p_bmp = SDL_LoadBMP("p.bmp");
    SDL_Surface* q_bmp = SDL_LoadBMP("q.bmp");
    SDL_Surface* r_bmp = SDL_LoadBMP("r.bmp");
    SDL_Surface* s_bmp = SDL_LoadBMP("s.bmp");
    SDL_Surface* t_bmp = SDL_LoadBMP("t.bmp");
    SDL_Surface* u_bmp = SDL_LoadBMP("u.bmp");
    SDL_Surface* v_bmp = SDL_LoadBMP("v.bmp");
    SDL_Surface* x_bmp = SDL_LoadBMP("x.bmp");
    SDL_Surface* y_bmp = SDL_LoadBMP("y.bmp");
    SDL_Surface* w_bmp = SDL_LoadBMP("w.bmp");
    SDL_Surface* z_bmp = SDL_LoadBMP("z.bmp");
    SDL_Surface* n_bmp = SDL_LoadBMP("n.bmp");
     SDL_Surface* a1_bmp = SDL_LoadBMP("1.bmp");
    SDL_Surface* a2_bmp = SDL_LoadBMP("2.bmp");
    SDL_Surface* a3_bmp = SDL_LoadBMP("3.bmp");
    SDL_Surface* a4_bmp = SDL_LoadBMP("4.bmp");
    SDL_Surface* a5_bmp = SDL_LoadBMP("5.bmp");
    SDL_Surface* a6_bmp = SDL_LoadBMP("6.bmp");
    SDL_Surface* a7_bmp = SDL_LoadBMP("7.bmp");
    SDL_Surface* a8_bmp = SDL_LoadBMP("8.bmp");
    SDL_Surface* a9_bmp = SDL_LoadBMP("9.bmp");
    SDL_Surface* a10_bmp = SDL_LoadBMP("10.bmp");
    SDL_Surface* a11_bmp = SDL_LoadBMP("11.bmp");
    SDL_Surface* a12_bmp = SDL_LoadBMP("12.bmp");
    SDL_Surface* a13_bmp = SDL_LoadBMP("13.bmp");
    SDL_Surface* a14_bmp = SDL_LoadBMP("14.bmp");
    SDL_Surface* a15_bmp = SDL_LoadBMP("15.bmp");
    SDL_Surface* a16_bmp = SDL_LoadBMP("16.bmp");
    SDL_Surface* a17_bmp = SDL_LoadBMP("17.bmp");
    SDL_Surface* a18_bmp = SDL_LoadBMP("18.bmp");
    SDL_Surface* a19_bmp = SDL_LoadBMP("19.bmp");
    SDL_Surface* a20_bmp = SDL_LoadBMP("20.bmp");
    SDL_Surface* a21_bmp = SDL_LoadBMP("21.bmp");
    SDL_Surface* a22_bmp = SDL_LoadBMP("22.bmp");
    SDL_Surface* a23_bmp = SDL_LoadBMP("23.bmp");
    SDL_Surface* a24_bmp = SDL_LoadBMP("24.bmp");
    SDL_Surface* a25_bmp = SDL_LoadBMP("25.bmp");
    SDL_Surface* a26_bmp = SDL_LoadBMP("26.bmp");


    SDL_Surface* jeu2_bmp = SDL_LoadBMP("jeu2.bmp");
    SDL_Surface* jeu22_bmp = SDL_LoadBMP("jeu2.bmp");
    SDL_Surface* mots1_bmp = SDL_LoadBMP("mots1.bmp");
    SDL_Surface* mots2_bmp = SDL_LoadBMP("mots2.bmp");



    position_menu.x =0;
    position_menu.y =0;
    posp.x=0;
    posp.y=0;
    posm.x=200;
    posm.y=400;





    m=0;
    c=0;
    a=0;
    j=0;
    j2=0;
    l=0;
    v=0;



    while (continuer)
    {//if(Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, MIX_DEFAULT_CHANNELS, 1024) == -1) //Initialisation de l'API Mixer
    //{
      //  printf("%s", Mix_GetError());

   // }
   //  musique = Mix_LoadMUS(musiquePath); //Chargement de la musique
    //if(musique == NULL)
    //{
      //  printf("Erreur loading musique %s",Mix_GetError());

   // }
     //   Mix_PlayMusic(musique, -1);

        while(SDL_PollEvent(&event))
        {
            switch (event.type)
            {
            case SDL_QUIT:
                continuer = 0;
            case SDL_KEYDOWN:
            {
                if (event.key.keysym.sym == SDLK_ESCAPE && m==0 && a==0 &&c==0)
                    continuer=0;
                if (event.key.keysym.sym == SDLK_ESCAPE && ( a!=0 || c!=0 || j!=0 || l!=0))
                {

                    a=0;
                    c=0;
                    j=0;
                    l=0;
                    j2=0;


                }
                if (event.key.keysym.sym == SDLK_BACKSPACE)
                    continuer=0;
                if (event.key.keysym.sym == SDLK_RETURN && m==2)
                    continuer=0;

                if (event.key.keysym.sym == SDLK_RETURN && m==1)

                    a++;

                if (event.key.keysym.sym == SDLK_DOWN && a==0  && c==0)
                    m++;
                if (event.key.keysym.sym == SDLK_UP && a==0 && c==0)
                    m--;
                    if (event.key.keysym.sym == SDLK_RETURN && m==0 && c==0)
                        c++;
                    if (event.key.keysym.sym == SDLK_DOWN && m==0 && c!=0&&j==0&&j2==0)
                        {if(c==2)
                            c=1;
                            else
                                c++;
                       }
                    if (event.key.keysym.sym == SDLK_UP && m==0 && c!=0&&j==0&&j2==0)
                        if(c==1)
                        c=2;
                        else c--;
                    if (event.key.keysym.sym == SDLK_p && m==0 && c==1 && j==0 )
                        j++;
                    if (event.key.keysym.sym == SDLK_p && m==0 && c==2 && j2==0)
                        j2++;



break;
            }
           case SDL_MOUSEBUTTONDOWN:
                   {if(j==1 || j2==1)
                     {
                   int x=event.motion.x;
                   int y=event.motion.y;
                   printf("x=%d",x);
                   printf("Y=%d",y);
                   if(x<100)
                   {
                       if(y<100)
                        {l=1;
                        posp.x=0;
                        posp.y=0;

                        }

                       if(y<200&&y>100)
                        {l=2;
                        posp.x=0;
                        posp.y=100;

                        }
                       if(y<300&&y>200)
                       {
                           l=3;
                           posp.x=0;
                           posp.y=200;

                       }
                       if(y<400&&y>300)
                       {
                           l=4;
                           posp.x=0;
                           posp.y=300;

                       }
                       if(y<500&&y>400)
                       {
                           l=5;
                           posp.x=0;
                           posp.y=400;

                       }
                   }
                   if(x<200&&x>100)
                   {
                     if(y<100)
                        {l=6;
                        posp.x=100;
                        posp.y=0;
                        }

                       if(y<200&&y>100)
                        {l=7;
                        posp.x=100;
                        posp.y=100;
                        }
                       if(y<300&&y>200)
                       {
                           l=8;
                           posp.x=100;
                           posp.y=200;

                       }
                       if(y<400&&y>300)
                       {
                           l=9;
                           posp.x=100;
                           posp.y=300;

                       }
                       if(y<500&&y>400)
                       {
                           l=10;
                           posp.x=100;
                           posp.y=400;

                       }
                   }
                   if(x<300&&x>200)
                   {
                     if(y<100)
                        {l=11;
                        posp.x=200;
                        posp.y=0;
                        }

                       if(y<200&&y>100)
                        {l=12;
                        posp.x=200;
                        posp.y=100;
                        }
                       if(y<300&&y>200)
                       {
                           l=13;
                           posp.x=200;
                           posp.y=200;

                       }
                       if(y<400&&y>300)
                       {
                           l=14;
                           posp.x=200;
                           posp.y=300;

                       }

                   }
                   if(x<400&&x>300)
                   {
                     if(y<100)
                        {l=15;
                        posp.x=300;
                        posp.y=0;
                        }

                       if(y<200&&y>100)
                        {l=16;
                        posp.x=300;
                        posp.y=100;
                        }
                       if(y<300&&y>200)
                       {
                           l=17;
                           posp.x=300;
                           posp.y=200;

                       }
                       if(y<400&&y>300)
                       {
                           l=18;
                           posp.x=300;
                           posp.y=300;

                       }


                   }
                    if(x<500&&x>400)
                   {
                     if(y<100)
                        {l=19;
                        posp.x=400;
                        posp.y=0;
                        }

                       if(y<200&&y>100)
                        {l=20;
                        posp.x=400;
                        posp.y=100;
                        }
                       if(y<300&&y>200)
                       {
                           l=21;
                           posp.x=400;
                           posp.y=200;

                       }
                       if(y<400&&y>300)
                       {
                           l=22;
                           posp.x=400;
                           posp.y=300;

                       }


                   }
                   if(x<600&&x>500)
                   {
                     if(y<100)
                        {l=23;
                        posp.x=500;
                        posp.y=0;
                        }

                       if(y<200&&y>100)
                        {l=24;
                        posp.x=500;
                        posp.y=100;
                        }
                       if(y<300&&y>200)
                       {
                           l=25;
                           posp.x=500;
                           posp.y=200;

                       }
                       if(y<400&&y>300)
                       {
                           l=26;
                           posp.x=500;
                           posp.y=300;

                       }


                   }

                   }


                    break;
                   }

            }




        }

          //

        SDL_BlitSurface(menu_bmp, NULL, ecran, &position_menu);
        m=m%3;
        a=a%2;
        c=c%3;
        if(l==0)
        {
            SDL_BlitSurface(jeu11_bmp, NULL, jeu1_bmp, &position_menu);
            SDL_BlitSurface(jeu22_bmp, NULL, jeu2_bmp, &position_menu);
        }
        if(m == 0 )
            {SDL_BlitSurface(menu_bmp, NULL, ecran, &position_menu);

            if(c==1)
             {SDL_BlitSurface(niveau1_bmp, NULL, ecran, &position_menu);
               if( j==1)
              {SDL_BlitSurface(jeu1_bmp, NULL, ecran, &position_menu);
              SDL_BlitSurface(mots1_bmp, NULL, jeu11_bmp, &posm);
                    switch(l)
                {
                    case 1:SDL_BlitSurface(a_bmp, NULL, ecran, &posp);break;
                    case 2:SDL_BlitSurface(b_bmp, NULL, ecran, &posp);break;
                    case 3:SDL_BlitSurface(x_bmp, NULL, ecran, &posp);break;
                    case 4:SDL_BlitSurface(h_bmp, NULL, ecran, &posp);break;
                    case 5:SDL_BlitSurface(t_bmp, NULL, jeu1_bmp, &posp);break;
                    case 6:SDL_BlitSurface(z_bmp, NULL, ecran, &posp);break;
                    case 7:SDL_BlitSurface(d_bmp, NULL, ecran, &posp);break;
                    case 8:SDL_BlitSurface(p_bmp, NULL, jeu1_bmp, &posp);break;
                    case 9:SDL_BlitSurface(e_bmp, NULL, jeu1_bmp, &posp);break;
                    case 10:SDL_BlitSurface(s_bmp, NULL, jeu1_bmp, &posp);break;
                    case 11:SDL_BlitSurface(k_bmp, NULL, ecran, &posp);break;
                    case 12:SDL_BlitSurface(w_bmp, NULL, ecran, &posp);break;
                    case 13:SDL_BlitSurface(j_bmp, NULL, ecran, &posp);break;
                    case 14:SDL_BlitSurface(l_bmp, NULL, ecran, &posp);break;
                    case 15:SDL_BlitSurface(r_bmp, NULL, jeu1_bmp, &posp);break;
                    case 16:SDL_BlitSurface(n_bmp, NULL, ecran, &posp);break;
                    case 17:SDL_BlitSurface(q_bmp, NULL, ecran, &posp);break;
                    case 18:SDL_BlitSurface(y_bmp, NULL, ecran, &posp);break;
                    case 19:SDL_BlitSurface(i_bmp, NULL, jeu1_bmp, &posp);break;
                    case 20:SDL_BlitSurface(c_bmp, NULL, ecran, &posp);break;
                    case 21:SDL_BlitSurface(u_bmp, NULL, ecran, &posp);break;
                    case 22:SDL_BlitSurface(f_bmp, NULL, ecran, &posp);break;
                    case 23:SDL_BlitSurface(v_bmp, NULL, ecran, &posp);break;
                    case 24:SDL_BlitSurface(g_bmp, NULL, ecran, &posp);break;
                    case 25:SDL_BlitSurface(m_bmp, NULL, ecran, &posp);break;
                    case 26:SDL_BlitSurface(o_bmp, NULL, ecran, &posp);break;

                }





              }



        }
            if(c==2)
                {
                    SDL_BlitSurface(niveau2_bmp, NULL, ecran, &position_menu);
                    if(j2==1)
                    {
                      SDL_BlitSurface(jeu2_bmp, NULL, ecran, &position_menu);
                      SDL_BlitSurface(mots2_bmp, NULL, jeu22_bmp, &posm);

                      switch(l)
                {
                    case 1:SDL_BlitSurface(a3_bmp, NULL, ecran, &posp);break;
                    case 2:SDL_BlitSurface(a17_bmp, NULL, ecran, &posp);break;
                    case 3:SDL_BlitSurface(a18_bmp, NULL, ecran, &posp);break;
                    case 4:SDL_BlitSurface(a4_bmp, NULL, jeu2_bmp, &posp);break;
                    case 5:SDL_BlitSurface(a21_bmp, NULL, ecran, &posp);break;
                    case 6:SDL_BlitSurface(a1_bmp, NULL, ecran, &posp);break;
                    case 7:SDL_BlitSurface(a26_bmp, NULL, ecran, &posp);break;
                    case 8:SDL_BlitSurface(a2_bmp, NULL, ecran, &posp);break;
                    case 9:SDL_BlitSurface(a23_bmp, NULL, jeu2_bmp, &posp);break;
                    case 10:SDL_BlitSurface(a5_bmp, NULL, ecran, &posp);break;
                    case 11:SDL_BlitSurface(a16_bmp, NULL, ecran, &posp);break;
                    case 12:SDL_BlitSurface(a11_bmp, NULL, jeu2_bmp, &posp);break;
                    case 13:SDL_BlitSurface(a7_bmp, NULL, ecran, &posp);break;
                    case 14:SDL_BlitSurface(a10_bmp, NULL, ecran, &posp);break;
                    case 15:SDL_BlitSurface(a15_bmp, NULL, ecran, &posp);break;
                    case 16:SDL_BlitSurface(a6_bmp, NULL, ecran, &posp);break;
                    case 17:SDL_BlitSurface(a24_bmp, NULL, jeu2_bmp, &posp);break;
                    case 18:SDL_BlitSurface(a20_bmp, NULL, ecran, &posp);break;
                    case 19:SDL_BlitSurface(a19_bmp, NULL, ecran, &posp);break;
                    case 20:SDL_BlitSurface(a12_bmp, NULL, ecran, &posp);break;
                    case 21:SDL_BlitSurface(a8_bmp, NULL, ecran, &posp);break;
                    case 22:SDL_BlitSurface(a13_bmp, NULL, ecran, &posp);break;
                    case 23:SDL_BlitSurface(a9_bmp, NULL, ecran, &posp);break;
                    case 24:SDL_BlitSurface(a22_bmp, NULL, ecran, &posp);break;
                    case 25:SDL_BlitSurface(a14_bmp, NULL, jeu2_bmp, &posp);break;
                    case 26:SDL_BlitSurface(a25_bmp, NULL, ecran, &posp);break;

                }



              }


                }
            }

        if(m == 1)
        {
            SDL_BlitSurface(menu1_bmp, NULL, ecran, &position_menu);
            if(a == 1)
              SDL_BlitSurface(aide_bmp, NULL, ecran, &position_menu);

            }
         if (m == 2)
            SDL_BlitSurface(menu2_bmp, NULL, ecran, &position_menu);



        SDL_Flip(ecran);

            }

}
